
#include "Quad.h"
#include "Vertex.h"

//
// Constructor
//

Quad::Quad(uint _a, uint _b, uint _c, uint _d)
{
	a = a2 = _a;
	b = _b;
	c = c2 = _c;
	d = _d;
}

//
// Set
//

void Quad::Set(uint _a, uint _b, uint _c, uint _d)
{
	a = a2 = _a;
	b = _b;
	c = c2 = _c;
	d = _d;
}

//
// Update2s
//

void Quad::Update2s()
{
	a2 = a;
	c2 = c;
}

//
// Midpoints
//

Real3 Quad::MidPoint(vector<Vertex> *vertices) const
{
	Real3 p = vertices->at(a).position + 
						vertices->at(b).position + 
						vertices->at(c).position + 
						vertices->at(d).position;
	p *= 0.25f;

	return p;
}

Real3 Quad::Side0MidPoint(vector<Vertex> *vertices) const
{
	return (vertices->at(a).position + vertices->at(b).position) * 0.5f;
}

Real3 Quad::Side1MidPoint(vector<Vertex> *vertices) const
{
	return (vertices->at(c).position + vertices->at(b).position) * 0.5f;
}

Real3 Quad::Side2MidPoint(vector<Vertex> *vertices) const
{
	return (vertices->at(d).position + vertices->at(c).position) * 0.5f;
}

Real3 Quad::Side3MidPoint(vector<Vertex> *vertices) const
{
	return (vertices->at(a).position + vertices->at(d).position) * 0.5f;
}

//
// Normals
//

Real3 Quad::Normal(vector<Vertex> *vertices) const
{
	return Cross(NormalOf(vertices->at(b).position - vertices->at(a).position),
							 NormalOf(vertices->at(c).position - vertices->at(a).position));
}

Real3 Quad::Side0Normal(vector<Vertex> *vertices) const
{
	return (vertices->at(a).normal + vertices->at(b).normal) * 0.5f;
}

Real3 Quad::Side1Normal(vector<Vertex> *vertices) const
{
	return (vertices->at(c).normal + vertices->at(b).normal) * 0.5f;
}

Real3 Quad::Side2Normal(vector<Vertex> *vertices) const
{
	return (vertices->at(d).normal + vertices->at(c).normal) * 0.5f;
}

Real3 Quad::Side3Normal(vector<Vertex> *vertices) const
{
	return (vertices->at(a).normal + vertices->at(d).normal) * 0.5f;
}

//
// Texture Coordinates
//

Real2 Quad::Tex(vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	Real2 t = vertices->at(a).tex + 
						vertices->at(b).tex + 
						vertices->at(c).tex + 
						vertices->at(d).tex;
	t *= 0.25f;

	return t;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Quad::Side0Tex(vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(a).tex + vertices->at(b).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Quad::Side1Tex(vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(b).tex + vertices->at(c).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Quad::Side2Tex(vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(c).tex + vertices->at(d).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Quad::Side3Tex(vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(d).tex + vertices->at(a).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}
